// components/dropdownMenu/dropdownMenu.js
Component({
	options: {
		addGlobalClass: true,
		multipleSlots: true, // 在组件定义时的选项中启用多slot支持
	},
	relations: {
		'../dropdown/dropdown': {
			type: 'parent', // 关联的目标节点应为子节点
			linked: function (target) {
				// 每次有custom-li被插入时执行，target是该节点实例对象，触发在该节点attached生命周期之后
				// console.log(target)
				// console.log(target.data)
			},
			linkChanged: function (target) {
				// 每次有custom-li被移动后执行，target是该节点实例对象，触发在该节点moved生命周期之后
			},
			unlinked: function (target) {
				// 每次有custom-li被移除时执行，target是该节点实例对象，触发在该节点detached生命周期之后
				// console.log(target)
			},
		},
		'../dropdownItem/dropdownItem': {
			type: 'child', // 关联的目标节点应为子节点
			linked: function (target) {
				// 每次有custom-li被插入时执行，target是该节点实例对象，触发在该节点attached生命周期之后
				// console.log(target)
				// console.log(target.data)
			},
			linkChanged: function (target) {
				// 每次有custom-li被移动后执行，target是该节点实例对象，触发在该节点moved生命周期之后
			},
			unlinked: function (target) {
				// 每次有custom-li被移除时执行，target是该节点实例对象，触发在该节点detached生命周期之后
				// console.log(target)
			},
		},
	},
	/**
	 * 组件的属性列表
	 */
	properties: {
		value: {
			type: String,
			value: '',
			observer: function (n, o) {
				// console.log(n, o)
				let children = this.data.children || []
				children.map(v => {
					v.judgeActived()
				})
			},
		},
		title: { type: String, value: '' },
	},

	/**
	 * 组件的初始数据
	 */
	data: {
		index: null,
		open: false,
		parent: null,
		children: [],
		label: '',
		width: null,
	},

	ready() {
		this.getList()
	},
	/**
	 * 组件的方法列表
	 */
	methods: {
		getList: function () {
			var nodes = this.getRelationNodes('../dropdown/dropdown')
			let parent = nodes[0]
			var children = this.getRelationNodes('../dropdownItem/dropdownItem')
			this.setData({
				parent,
				children,
			})
			children.map(v => {
				v.judgeActived()
			})
		},
		setValue(value, label) {
			this.setData({
				value,
				label,
			})
			this.clickFn()
			this.triggerEvent('change', value)
		},
		clickFn() {
			let open = !this.data.open
			this.setData({ open })
			open ? this.data.parent.setOpen(Number(this.data.index)) : null
		},
	},
})
